//
//  DMVisitor.swift
//  DM_新浪微博
//
//  Created by Damon.JiAo on 16/2/25.
//  Copyright © 2016年 Demon_JiAo. All rights reserved.
//

import UIKit
import SnapKit

protocol DMVisitorDelegate:NSObjectProtocol{
    
    func loginClick()
    
    func registerClick()
}

class DMVisitor: UIView {

    weak var delegate:DMVisitorDelegate?
    
    /** 1.第一步：重写构造函数 */
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /** 设置整体背景颜色 */
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        setInfo()
        
    }
    
    /** 针对不同的控制器 -- 提供一个借口设置不同的属性 */
    func setInfoSetting(message:String,image:String,anmintion:Bool){
        
        messageLabel.text = message
        
        iconImageView.image = UIImage(named: image)
        
        homeIconImageView.hidden = anmintion
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 2.第二步：创建一个私有方法，设置视图和约束 */
    private func setInfo(){
        
        /** 添加各个控件 */
        addSubview(homeIconImageView)
        /** 设置遮罩位置 */
        addSubview(coverView)
        
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(LoginButton)
        
        
        /** 设置各个子控件的约束！ */
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeIconImageView.snp_bottom).offset(10)
            make.centerX.equalTo(self.snp_centerX)
            make.width.equalTo(216)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        LoginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        coverView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    
    /** 设置滚动图画是否执行 */
    func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 5
        
        animation.removedOnCompletion = false
        
        homeIconImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    /** 3.第三步：懒加载 -- 添加各种视图的View（按钮） */

    //  首页动态图片
    private lazy var homeIconImageView:UIImageView = {
       
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
    }()
    
    //  通用图片
    private lazy var iconImageView:UIImageView = {
       
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
    }()
    
    //  说明Label
    private lazy var messageLabel:UILabel = {
       
        let label = UILabel()
        
        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        return label
    }()
    
    //  注册按钮属性
    private lazy var registerButton:UIButton = {
       
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /** 注册按钮点击事件（代理） */
    func register(){
        delegate?.registerClick()
    }
    
    //  登录按钮属性
    private lazy var LoginButton:UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //  登录按钮点击事件（代理）
    func login(){
        
        delegate?.loginClick()
        
    }
    
    
    //  设置阴影遮罩
    private lazy var coverView:UIImageView = {
       
        let cover = UIImageView()
        
        cover.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return cover
    }()
    
    
}
